var companyId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.companyId);
var schoolId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolId);
var schoolMap = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolMap);

var app = new Vue({
    el: '#app',
    data: {
        permissions:['activitySales:list','activitySales:add','activitySales:update','activitySales:delete'
            ,'activitySales:addClass','activitySales:deleteClass'],//本页权限数组
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if(empty(token)){
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon:5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            },1200);

            return ;
        }
    },
    methods: {
        //权限控制
        permission: function (index) {
            return (_PERMISSIONS.indexOf(this.permissions[index]) != -1);
        },
        //添加
        add:function (indexs) {
            var that = this;
            var type = 2;
            if (indexs == 4) {
                // 添加大类
                type = 1;
            }
            var index = layer.open({
                type: 2,
                title: "新增",
                content: "edit.html?id=&type="+type,
                fix: true,
                shadeClose: true,
                shade: false,
                area: ['70%', '70%'],
                maxmin: true, //开启最大化最小化按钮
            });
            layer.full(index);
        },
        //修改
        update:function () {
            var that = this;
            var list = $('#menuTable').bootstrapTreeTable('getSelections');
            if (list.length <= 0 || list.length > 1) {
                layer.msg('请选中一条数据', {time: 1500, icon: 5});
                return;
            }
            list = list[0];
            var index = layer.open({
                type: 2,
                title: "修改",
                content: "edit.html?id="+list.id,
                fix: true,
                shadeClose: true,
                shade: false,
                area: ['70%', '70%'],
                maxmin: true, //开启最大化最小化按钮
            });
            layer.full(index);
        },
        //删除
        del:function (index) {
            var that = this;
            var list = $('#menuTable').bootstrapTreeTable('getSelections');
            if (list.length <= 0) {
                layer.msg('请选中一条数据', {time: 1500, icon: 5});
                return;
            }

            var ids = "";
            var flag2 = true;
            for (let i = 0; i < list.length; i++) {
                ids += list[i].id;
                if ((i+1)!=list.length) {
                    ids += ",";
                }
                if (index!=5) {
                    if (1 == list[i].type) {
                        flag2 = false;
                        break;
                    }
                }
            }
            // 判断是否有权操作
            if (!flag2) {
                layer.msg('删除大类权限不足，无法操作!', {time: 1500, icon: 5});
                return;
            }

            var confirm = layer.confirm('确定删除选中？', {
                btn: ['确定', '取消'], //按钮
                shadeClose: false,
            }, function () {
                Put(that,_HOST+_API.actArt_activitySales_delete+"?ids="+ids,{},true,that.permissions[3])
                    .then(function (response) {
                        // 响应成功回调
                        console.log(response);
                        layer.close(confirm);
                        if(response){
                            if(response.success){
                                layer.msg(response.msg, {time: 1500, icon:1});
                                setTimeout(function () {
                                    window.location.reload(); //刷新页面
                                },1200)
                            }else {
                                layer.msg(response.msg, {time: 1500, icon:5});
                            }
                        }
                    })
            });

        },
        refresh:function (index) {
            window.location.reload(); //刷新页面
        }
    },
});

$(function () {
    var treeTable = $('#menuTable').bootstrapTreeTable({
        parentCode : 'parentId',// 用于设置父子关系
        type: 'get',                   // 请求方式（*）
        url: _HOST+_API.actArt_activitySales_list+"?companyId="+companyId,            // 请求后台的URL（*）
        ajaxParams : {},// 请求数据的ajax的data属性
        headers: {
            'Authorization':sessionStorageUtils.getJson(storageKey.sessionStorageKey.token),
            'permCoding':'activitySales:list'
        },
        toolbar: "#toolbar",      //顶部工具条
        expandColumn : 2,              // 在哪一列上面显示展开按钮
        expandFirst : false, // 是否默认第一级展开--expandAll为false时生效
        columns: [
            {
                field: 'selectItem',
                checkbox: true
            },
            {
                field: 'id',
                title: 'ID',
                align: 'center',
                valign: 'middle',
                width: '60px'
            },
            {
                field: 'name',
                title: '名称',
                align: 'center',
                valign: 'middle'
            },
            {
                field: 'price',
                title: '默认收费(元/人)',
                align: 'center',
                valign: 'middle'
            },
            {
                field: 'type',
                title: '类型',
                align: 'center',
                valign: 'middle',
                width: '100px',
                formatter: function (value, index) {
                    if (value=== 1) {
                        return '<span class="label label-primary">售票大类</span>';
                    }
                    if (value === 2) {
                        return '<span class="label label-success">售票类型</span>';
                    }
                    return '-';
                }
            },
            {
                field: 'sort',
                title: '排序号',
                align: 'center',
                valign: 'middle',
                width: '100px',
            },
            {
                field: 'gmtCreate',
                title: '创建时间',
                align: 'center',
                width: '150px',
                formatter: function (value, index) {
                    return formatDateTime(value);
                }
            }
        ]
    });
});
